﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace LCWMS
{
    public partial class w_inv001 : DockContent
    {
        DataSet dscustomer;
        //记录升降序
        //private bool b_sort; //true升序，否则降序
        private bool b_putaway_sort;
        private int i_sku_header;
        private int i_sku_fix_header;
        private int i_location_header;
        private int i_batchno_header;
        private int i_pallet_header;
        private string g_str_putaway;
        private string g_str_ship;

        private string s_enter_flag;  //load的时候不执行enter事件 
        public static string s_pop_location_code; // 页面跳转

        public w_inv001()
        {
            InitializeComponent();
            //DataGridView不自动生成列
            dvg_pallet.AutoGenerateColumns = false;
            dvg_location_code.AutoGenerateColumns = false;
            dgv_sku.AutoGenerateColumns = false;
            dgv_sku_fix.AutoGenerateColumns = false;
            dgv_batchno.AutoGenerateColumns = false;
        }
        //添加商品库存的合计行
        private void f_add_sku()
        {
            decimal d_1, d_2, d_3, d_4, d_5, d_6, d_7, d_8, d_9;
            int i_1, i_2, i_3;
            DataTable dt_put;
            //增加一个合计行 
            dt_put = (DataTable)dgv_sku.DataSource;
            //d_1 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true");
            //d_2 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_s"].ColumnName + ")", "true");
            //d_3 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_m"].ColumnName + ")", "true");
            //d_4 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_s"].ColumnName + ")", "true");
            //d_5 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_m"].ColumnName + ")", "true");
            //d_6 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_s"].ColumnName + ")", "true");
            //d_7 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_m"].ColumnName + ")", "true");
            //d_8 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_s"].ColumnName + ")", "true");
            //d_9 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["box"].ColumnName + ")", "true");

            i_1 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true"));
            i_2 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_m"].ColumnName + ")", "true"));
            i_3 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["ky_qty_m"].ColumnName + ")", "true"));

            DataRow dr = dt_put.NewRow();
            dr["sku"] = "合计";
            dr["stock_qty_m"] = i_1;
            //dr["stock_qty_s"] = d_2;
            //dr["ungot_qty_m"] = d_3;
            //dr["ungot_qty_s"] = d_4;
            dr["allocate_qty_m"] = i_2;
            dr["ky_qty_m"] = i_3;
            //dr["allocate_qty_s"] = d_6;
            //dr["freeze_qty_m"] = d_7;
            //dr["freeze_qty_s"] = d_8;
            //dr["box"] = d_9;
            ((DataTable)dgv_sku.DataSource).Rows.InsertAt(dr, ((DataTable)dgv_sku.DataSource).Rows.Count);
        }
        //添加商品库存的合计行
        private void f_add_sku_fix()
        {
            decimal d_1, d_2, d_5, d_6, d_7, d_8, d_9, d_10, d_11, d_12, d_13, d_14, d_15, d_16;
            DataTable dt_put;
            int i_1, i_2, i_3;
            //增加一个合计行 
            dt_put = (DataTable)dgv_sku_fix.DataSource;

            i_1 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true"));
            d_2 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_s"].ColumnName + ")", "true");
            //d_3 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_m"].ColumnName + ")", "true");
            //d_4 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_s"].ColumnName + ")", "true");
            //d_5 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_m"].ColumnName + ")", "true");
            i_2 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_m"].ColumnName + ")", "true"));
            i_3 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["ky_qty_m"].ColumnName + ")", "true"));
            d_6 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_s"].ColumnName + ")", "true");
            d_7 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_m"].ColumnName + ")", "true");
            d_8 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_s"].ColumnName + ")", "true");

            d_9 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishin_qty_m"].ColumnName + ")", "true");
            d_10 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishin_qty_s"].ColumnName + ")", "true");
            d_11 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishout_qty_m"].ColumnName + ")", "true");
            d_12 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishout_qty_s"].ColumnName + ")", "true");
            d_13 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["movein_qty_m"].ColumnName + ")", "true");
            d_14 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["movein_qty_s"].ColumnName + ")", "true");
            d_15 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["moveout_qty_m"].ColumnName + ")", "true");
            d_16 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["moveout_qty_s"].ColumnName + ")", "true");
            //d_17 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["box"].ColumnName + ")", "true");
            DataRow dr = dt_put.NewRow();
            dr["sku"] = "合计";
            dr["stock_qty_m"] = i_1;
            dr["stock_qty_s"] = d_2;
            //dr["ungot_qty_m"] = d_3;
            //dr["ungot_qty_s"] = d_4;
            dr["allocate_qty_m"] = i_2;
            dr["ky_qty_m"] = i_3;
            dr["allocate_qty_s"] = d_6;
            dr["freeze_qty_m"] = d_7;
            dr["freeze_qty_s"] = d_8;

            dr["replenishin_qty_m"] = d_9;
            dr["replenishin_qty_s"] = d_10;
            dr["replenishout_qty_m"] = d_11;
            dr["replenishout_qty_s"] = d_12;
            dr["movein_qty_m"] = d_13;
            dr["movein_qty_s"] = d_14;
            dr["moveout_qty_m"] = d_15;
            dr["moveout_qty_s"] = d_16;
            //dr["box"] = d_17;
            ((DataTable)dgv_sku_fix.DataSource).Rows.InsertAt(dr, ((DataTable)dgv_sku_fix.DataSource).Rows.Count);
        }

        //添加库位库存的合计行
        private void f_add_location()
        {
            decimal d_1, d_2,  d_5, d_6, d_7, d_8, d_9, d_10, d_11, d_12, d_13, d_14, d_15, d_16;
            DataTable dt_put;
            int i_1, i_2, i_3;
            //增加一个合计行 
            dt_put = (DataTable)dvg_location_code.DataSource;

            i_1 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true"));
            d_2 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_s"].ColumnName + ")", "true");
            //d_3 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_m"].ColumnName + ")", "true");
            //d_4 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_s"].ColumnName + ")", "true");
            //d_5 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_m"].ColumnName + ")", "true");
            i_2 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_m"].ColumnName + ")", "true"));
            i_3 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["ky_qty_m"].ColumnName + ")", "true"));
            d_6 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["allocate_qty_s"].ColumnName + ")", "true");
            d_7 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_m"].ColumnName + ")", "true");
            d_8 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_s"].ColumnName + ")", "true");

            d_9 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishin_qty_m"].ColumnName + ")", "true");
            d_10 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishin_qty_s"].ColumnName + ")", "true");
            d_11 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishout_qty_m"].ColumnName + ")", "true");
            d_12 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["replenishout_qty_s"].ColumnName + ")", "true");
            d_13 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["movein_qty_m"].ColumnName + ")", "true");
            d_14 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["movein_qty_s"].ColumnName + ")", "true");
            d_15 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["moveout_qty_m"].ColumnName + ")", "true");
            d_16 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["moveout_qty_s"].ColumnName + ")", "true");
            //d_17 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["box"].ColumnName + ")", "true");
            DataRow dr = dt_put.NewRow();
            dr["sku"] = "合计";
            dr["stock_qty_m"] = i_1;    
            dr["stock_qty_s"] = d_2;
            //dr["ungot_qty_m"] = d_3;
            //dr["ungot_qty_s"] = d_4;
            dr["allocate_qty_m"] = i_2;
            dr["ky_qty_m"] = i_3;
            dr["allocate_qty_s"] = d_6;
            dr["freeze_qty_m"] = d_7;
            dr["freeze_qty_s"] = d_8;

            dr["replenishin_qty_m"] = d_9;
            dr["replenishin_qty_s"] = d_10;
            dr["replenishout_qty_m"] = d_11;
            dr["replenishout_qty_s"] = d_12;
            dr["movein_qty_m"] = d_13;
            dr["movein_qty_s"] = d_14;
            dr["moveout_qty_m"] = d_15;
            dr["moveout_qty_s"] = d_16;
            //dr["box"] = d_17;
            ((DataTable)dvg_location_code.DataSource).Rows.InsertAt(dr, ((DataTable)dvg_location_code.DataSource).Rows.Count);
        }
        //添加批次库存的合计行
        private void f_add_batchno()
        {
            decimal d_1, d_2, d_5, d_6;
            int i_1;
            DataTable dt_put;
            //增加一个合计行 
            dt_put = (DataTable)dgv_batchno.DataSource;
            //d_1 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true");
            i_1 = Convert.ToInt32(dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true"));
            d_2 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_s"].ColumnName + ")", "true");
            d_5 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_m"].ColumnName + ")", "true");
            d_6 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_s"].ColumnName + ")", "true");
            //d_7 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["cubage"].ColumnName + ")", "true");
            //d_8 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["gross_weight"].ColumnName + ")", "true");
            //d_9 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["net_weight"].ColumnName + ")", "true");
            //d_10 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["box"].ColumnName + ")", "true");
            DataRow dr = dt_put.NewRow();
            dr["pack_code"] = "合计";
            dr["stock_qty_m"] = i_1;
            dr["stock_qty_s"] = d_2;
            dr["freeze_qty_m"] = d_5;
            dr["freeze_qty_s"] = d_6;
            //dr["cubage"] = d_7;
            //dr["gross_weight"] = d_8;
            //dr["net_weight"] = d_9;
            //dr["box"] = d_10;
            ((DataTable)dgv_batchno.DataSource).Rows.InsertAt(dr, ((DataTable)dgv_batchno.DataSource).Rows.Count);
        }
        //添加托盘库存的合计行
        private void f_add_pallet()
        {
            decimal d_1, d_2, d_3, d_4, d_5, d_6, d_7, d_8, d_9, d_10;
            DataTable dt_put;
            //增加一个合计行 
            dt_put = (DataTable)dvg_pallet.DataSource;
            d_1 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true");
            d_2 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_s"].ColumnName + ")", "true");
            d_3 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_m"].ColumnName + ")", "true");
            d_4 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["ungot_qty_s"].ColumnName + ")", "true");
            d_5 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_m"].ColumnName + ")", "true");
            d_6 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["freeze_qty_s"].ColumnName + ")", "true");
            d_7 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["cubage"].ColumnName + ")", "true");
            d_8 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["gross_weight"].ColumnName + ")", "true");
            d_9 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["net_weight"].ColumnName + ")", "true");
            d_10 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["box"].ColumnName + ")", "true");
            DataRow dr = dt_put.NewRow();
            dr["pack_code"] = "合计";
            dr["stock_qty_m"] = d_1;
            dr["stock_qty_s"] = d_2;
            dr["ungot_qty_m"] = d_3;
            dr["ungot_qty_s"] = d_4;
            dr["freeze_qty_m"] = d_5;
            dr["freeze_qty_s"] = d_6;
            dr["cubage"] = d_7;
            dr["gross_weight"] = d_8;
            dr["net_weight"] = d_9;
            dr["box"] = d_10;
            ((DataTable)dvg_pallet.DataSource).Rows.InsertAt(dr, ((DataTable)dvg_pallet.DataSource).Rows.Count);
        }
        private void w_inv001_Load(object sender, EventArgs e)
        {
            //获取按钮权限
            string str_right;
            string str_sql2 = "";
            DataSet ds_type;
            str_right = main.g_class.comm_getright("w_inv001", main.g_right_dataset);
            //导出
            if (str_right.Substring(0, 1) == "0")
            {
                btn_excel.Visible = false;
            }
            else
            {
                btn_excel.Visible = true;
            }
            dscustomer = new DataSet();
            try
            {

                dscustomer = main.g_class.comm_get_allconsignor();
                cb_consignor.DataSource = dscustomer.Tables[0];
                cb_consignor.ValueMember = "consignor";
                cb_consignor.DisplayMember = "consignor";
                main.g_class.comm_changeControl(this, main.g_win_dataset);
                //tabControl1.TabPages.Remove(tp_sku);//移出tp_batchno选项tab
                tabControl1.TabPages.Remove(tp_pallet);//移出tp_batchno选项tab
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information !", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }

            //得到是否显示未上架确认和未发货确认的库存
            g_str_putaway = "0";
            g_str_ship = "0";
            try
            {
                if (main.dbtype == "SQL")
                {
                    str_sql2 = "select isnull(id_data,'0') id_data from " + main.g_space + ".sys_parameter where inv_org='" + main.g_org + "'and id in (8300,8301) order by id";
                    ds_type = main.dbservice.Sql_exec_selectsql(str_sql2);
                }
                else
                {
                    str_sql2 = "select nvl(id_data,'0') id_data from " + main.g_space + ".sys_parameter where inv_org='" + main.g_org + "'and id in (8300,8301) order by id";
                    ds_type = main.dbservice.oracle_exec_selectsql(str_sql2);
                }
                if (ds_type.Tables[0].Rows.Count > 0)
                {
                    g_str_putaway = ds_type.Tables[0].Rows[0]["id_data"].ToString().Trim();
                    g_str_ship = ds_type.Tables[0].Rows[1]["id_data"].ToString().Trim();
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }

            s_enter_flag = "0";
            if (s_pop_location_code != null)
            {
                if (s_pop_location_code.Trim() != "")  //说明是快捷方式过来的
                {

                    tb_location_code.Text = s_pop_location_code;
                    s_enter_flag = "1";
                }
            }
            s_pop_location_code = "";
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {

            DataSet ds_Batch, ds_location, ds_sku, ds_sku_fix;
            string str_where = "where a.inv_org = '" + main.g_org + "' ";
            string str_sql_Batch = "";
            string str_sql_location = "";
            string str_sql_pallet = "";
            string str_sql_sku = "";
            string str_sql_sku_location = ""; // sku 子查询 根据库位的查询条件
            string str_sql_sku_fix = "";


            tabControl1.TabPages.Clear();
            tabControl1.TabPages.Add(tp_sku);
            tabControl1.TabPages.Add(tp_sku_fix);

            tabControl1.TabPages.Add(tp_location_code);
            //tabControl1.TabPages.Add(tp_pallet);
            tabControl1.TabPages.Add(tp_batch);

         
            if (main.dbtype == "SQL") 
            {

                //////// 商品明细 显示总库存
                //////str_sql_sku = "select RTRIM(a.sku) sku,RTRIM(b.sku_name) sku_name,RTRIM(b.barcode) barcode,RTRIM(b.standby_4)  as supply_code,RTRIM(b.fix_location) fix_location,  RTRIM(b.class_code3) class_code3," +
                //////    "(select RTRIM(class_name) as class_name from wms.mst_stat_class sc where sc.class_code = b.class_code3 and sc.class_flag='C3'   ) as class_name, " +
                //////    "case b.standby_3 when '10' then '正常' when '13' then '禁销' when '20' then '预淘汰' when '30' then '淘汰' when '0' then '新品' else '' end lifecycle_status_name," +  //10-正常、13-禁销、20-预淘汰、30-淘汰、0-新品

                //////    " RTRIM(a.pack_code) pack_code,c.unitsPerDelivery ,ISNULL(CAST(c.qty02 AS INT), 0) as box_spec, " + // 20241203 add 配送规格,箱规
                //////    " ISNULL(CAST(a.stock_qty_m AS INT), 0) stock_qty_m," +
                //////    " a.stock_qty_s," +
                //////    " a.ungot_qty_m," +
                //////    " a.ungot_qty_s," +
                //////    " ISNULL(CAST(a.stock_qty_m + a.ungot_qty_m - a.allocate_qty_m - a.freeze_qty_m AS INT), 0)  as ky_qty_m, " +  // --可用数量(库存+预收货-已分配-冻结)
                //////    " ISNULL(CAST(a.allocate_qty_m AS INT), 0) allocate_qty_m," +
                //////    " a.allocate_qty_s,a.freeze_qty_m,a.freeze_qty_s," +
                //////    " RTRIM(n.warehouse) warehouse,RTRIM(mw.warehouse_name) warehouse_name,"+
                //////    " b.spec,b.main_unit,b.sub_unit,b.warehouseman" + 
                //////    //",a.* ,isnull((select case c.spare_flag when '1' then round(a.stock_qty_m/c.qty05,0) else round(a.stock_qty_m/(c.qty02 * c.qty03),0) end  from " + main.g_space + ".mst_pack c where a.inv_org = c.inv_org and a.consignor =c.consignor and a.sku = c.sku and a.pack_code = c.pack_code),0)  as box  " + 
                //////    " from " + main.g_space + ".inv_sku a "+ 
                //////    " left join wms.mst_sku b  on a.inv_org = b.inv_org and a.consignor=b.consignor and a.sku=b.sku  " +
                //////    " LEFT JOIN WMS.mst_pack c on  a.inv_org = c.inv_org and a.consignor=c.consignor and a.pack_code = c.pack_code  " +
                //////    " LEFT JOIN WMS.mst_location m on  a.inv_org = m.inv_org AND b.fix_location = m.location_code  " +
                //////    " LEFT JOIN WMS.mst_area n on m.inv_org = n.inv_org AND m.area_code = n.area_code  " +
                //////    " LEFT JOIN WMS.mst_warehouse mw on n.inv_org = mw.inv_org and n.warehouse = mw.warehouse " +
                //////    " WHERE 1=1 and a.inv_org='" + main.g_org + "' ";

                //////str_sql_sku_location =
                //////  " SELECT distinct a.sku " +
                //////  " FROM  WMS.inv_sku_location a " +
                //////  " LEFT JOIN WMS.mst_sku b ON a.inv_org = b.inv_org  AND a.consignor = b.consignor   AND a.sku= b.sku " +
                //////  " LEFT JOIN WMS.mst_pack c ON a.inv_org = c.inv_org   AND a.consignor= c.consignor  AND a.pack_code = c.pack_code " +
                //////  " LEFT JOIN WMS.mst_location m ON a.inv_org = m.inv_org AND a.location_code = m.location_code " +
                //////  " LEFT JOIN WMS.mst_area n ON m.inv_org = n.inv_org  AND m.area_code = n.area_code " +
                //////  " LEFT JOIN WMS.mst_warehouse mw ON n.inv_org = mw.inv_org  AND n.warehouse = mw.warehouse  " +
                //////  " WHERE 1=1 and a.inv_org='" + main.g_org + "' ";



                // 1、商品明细 (显示商品各个库区的合计（正常库区合计多少个。退货库区多少个）)
                str_sql_sku = ""+
                        " select " +
                        "   RTRIM(n.warehouse) warehouse," +
                        "   RTRIM(mw.warehouse_name) warehouse_name," +
                        "   RTRIM(b.sku) sku," +
                        "   RTRIM(b.sku_name) sku_name," +
                        "   RTRIM(b.barcode) barcode," +
                        "   RTRIM(b.standby_4) AS supply_code," +
                        "  (select consignor_name as supply_name from wms.mst_customer where  b.standby_4 = consignor and supply_flag='1' ) supply_name, " +
                        "   RTRIM(b.fix_location) fix_location," +
                        "   RTRIM(b.class_code3) class_code3," +
                        "   (SELECT RTRIM(class_name) AS class_name FROM wms.mst_stat_class sc WHERE sc.class_code = b.class_code3 AND sc.class_flag= 'C3') AS class_name," +
                        "   case b.standby_3 when '10' then '正常' when '13' then '禁销' when '20' then '预淘汰' when '30' then '淘汰' when '0' then '新品' else '' end lifecycle_status_name," +  //10-正常、13-禁销、20-预淘汰、30-淘汰、0-新品
                        "   RTRIM(c.pack_code) pack_code," +
                        "   c.unitsPerDelivery ," + // 生命周期
                        "   ISNULL(CAST (c.qty02 AS INT), 0) AS box_spec," + // 20241203 add 配送规格,箱规
                        "   b.spec," +
                        "   b.main_unit," +
                        "   b.sub_unit," +
                        "   b.warehouseman ," +
                        "   sum(ISNULL(CAST (a.stock_qty_m AS INT), 0)) stock_qty_m," +
                        "   sum(a.stock_qty_s) stock_qty_s," +
                        "   sum(a.ungot_qty_m) ungot_qty_m," +
                        "   sum(a.ungot_qty_s) ungot_qty_s," +
                        "   sum(ISNULL(CAST (a.stock_qty_m + a.ungot_qty_m - a.allocate_qty_m - a.freeze_qty_m AS INT), 0)) AS ky_qty_m," + // --可用数量(库存+预收货-已分配-冻结)
                        "   sum(ISNULL(CAST (a.allocate_qty_m AS INT), 0)) allocate_qty_m," +
                        "   sum(a.allocate_qty_s) allocate_qty_s," +
                        "   sum(a.freeze_qty_m) freeze_qty_m," +
                        "   sum(a.freeze_qty_s) freeze_qty_s" +
                        " from wms.mst_sku b " +
                        " LEFT JOIN wms.inv_sku_location a ON a.inv_org = b.inv_org   AND a.consignor= b.consignor  AND a.sku= b.sku " +
                        "   LEFT JOIN WMS.mst_pack c ON b.inv_org = c.inv_org  " +
                        "   AND b.consignor= c.consignor  " +
                        "   AND b.sku = c.sku " +
                        " LEFT JOIN WMS.mst_location m ON b.inv_org = m.inv_org AND a.location_code = m.location_code " +
                        " LEFT JOIN WMS.mst_area n ON m.inv_org = n.inv_org AND m.area_code = n.area_code " +
                        " LEFT JOIN WMS.mst_warehouse mw ON n.inv_org = mw.inv_org AND n.warehouse = mw.warehouse  " +
                        " WHERE 1=1 and b.inv_org='" + main.g_org + "' ";

                // 2、商品库存（新增）：显示商品A位多少个，储位多少个，然后不用显示退货库的
                str_sql_sku_fix = "select RTRIM(b.sku_name) sku_name,RTRIM(b.barcode) barcode,RTRIM(b.standby_4)  as supply_code,RTRIM(b.class_code3) class_code3, " +
                                "(select RTRIM(class_name) as class_name from wms.mst_stat_class sc where sc.class_code = b.class_code3 and sc.class_flag='C3'   ) as class_name, " +
                                "RTRIM(b.spec) spec,RTRIM(b.brand) brand,RTRIM(b.main_unit) main_unit,RTRIM(b.sub_unit) sub_unit,RTRIM(b.warehouseman) warehouseman," +
                                "case b.standby_3 when '10' then '正常' when '13' then '禁销' when '20' then '预淘汰' when '30' then '淘汰' when '0' then '新品' else '' end lifecycle_status_name," +  //10-正常、13-禁销、20-预淘汰、30-淘汰、0-新品
                                "RTRIM(a.consignor) consignor,RTRIM(a.sku) sku,case a.location_code when 'QC001' then '待检区' else RTRIM(a.location_code) end location_code ,RTRIM(n.warehouse) warehouse,RTRIM(mw.warehouse_name) warehouse_name," +
                                "RTRIM(a.pack_code) pack_code,c.unitsPerDelivery ,ISNULL(CAST(c.qty02 AS INT), 0) as box_spec, " + // 20241203 add 配送规格,箱规
                                "b.fix_location, "+
                                "(select ISNULL(CAST(stock_qty_m + ungot_qty_m - allocate_qty_m - freeze_qty_m AS INT), 0)  as fix_ky_qty_m from WMS.inv_sku_location fml WHERE  fml.location_code = b.fix_location and fml.sku = a.sku) as fix_ky_qty_m, " + 
                                " ISNULL(CAST(a.stock_qty_m AS INT), 0) stock_qty_m," +
                                " a.stock_qty_s," +
                                " a.ungot_qty_m," +
                                " a.ungot_qty_s," +
                                " ISNULL(CAST(a.stock_qty_m + a.ungot_qty_m - a.allocate_qty_m - a.freeze_qty_m AS INT), 0)  as ky_qty_m, " +  // --可用数量(库存+预收货-已分配-冻结)
                                " ISNULL(CAST(a.allocate_qty_m AS INT), 0) allocate_qty_m," +
                                " a.allocate_qty_s,a.freeze_qty_m,a.freeze_qty_s,a.replenishin_qty_m,a.replenishin_qty_s,a.replenishout_qty_m,a.replenishout_qty_s,a.movein_qty_m,a.movein_qty_s,a.moveout_qty_m,a.moveout_qty_s" +
                                " from " + main.g_space + ".inv_sku_location a " +
                                " LEFT JOIN WMS.mst_sku b on  a.inv_org = b.inv_org AND a.consignor = b.consignor AND a.sku= b.sku  " +
                                " LEFT JOIN WMS.mst_pack c on  a.inv_org = c.inv_org and a.consignor=c.consignor and a.pack_code = c.pack_code  " +
                                " LEFT JOIN WMS.mst_location m on  a.inv_org = m.inv_org AND a.location_code = m.location_code  " +
                                " LEFT JOIN WMS.mst_area n on m.inv_org = n.inv_org AND m.area_code = n.area_code  " +
                                " LEFT JOIN WMS.mst_warehouse mw on n.inv_org = mw.inv_org and n.warehouse = mw.warehouse " +
                                " WHERE 1=1 and a.inv_org='" + main.g_org + "' " +
                                //" AND mw.warehouse <> '99804'" + // 不显示退货库库存
                                " AND a.location_code not like '04%' " + // 不显示退货库库存
                                " and a.location_code not in (select location_code from wms.mst_location where kind_flag=1 and location_code <> b.fix_location ) "; // 不显示原来A位的数据，只显示当前商品的A位和储位

                // 库位明细
                str_sql_location = "select RTRIM(b.sku_name) sku_name,RTRIM(b.barcode) barcode,RTRIM(b.standby_4)  as supply_code,RTRIM(b.class_code3) class_code3, " +
                                "(select RTRIM(class_name) as class_name from wms.mst_stat_class sc where sc.class_code = b.class_code3 and sc.class_flag='C3'   ) as class_name, " +
                                "RTRIM(b.spec) spec,RTRIM(b.brand) brand,RTRIM(b.main_unit) main_unit,RTRIM(b.sub_unit) sub_unit,RTRIM(b.warehouseman) warehouseman," +
                                "case b.standby_3 when '10' then '正常' when '13' then '禁销' when '20' then '预淘汰' when '30' then '淘汰' when '0' then '新品' else '' end lifecycle_status_name," +  //10-正常、13-禁销、20-预淘汰、30-淘汰、0-新品
                                "RTRIM(a.consignor) consignor,RTRIM(a.sku) sku,case a.location_code when 'QC001' then '待检区' else RTRIM(a.location_code) end location_code ,RTRIM(n.warehouse) warehouse,RTRIM(mw.warehouse_name) warehouse_name," +
                                "RTRIM(a.pack_code) pack_code,c.unitsPerDelivery ,ISNULL(CAST(c.qty02 AS INT), 0) as box_spec, " + // 20241203 add 配送规格,箱规
                                " ISNULL(CAST(a.stock_qty_m AS INT), 0) stock_qty_m," +
                                " a.stock_qty_s,"+
                                " a.ungot_qty_m," +
                                " a.ungot_qty_s," +
                                " ISNULL(CAST(a.stock_qty_m + a.ungot_qty_m - a.allocate_qty_m - a.freeze_qty_m AS INT), 0)  as ky_qty_m, " +  // --可用数量(库存+预收货-已分配-冻结)
                                " ISNULL(CAST(a.allocate_qty_m AS INT), 0) allocate_qty_m," +
                                " a.allocate_qty_s,a.freeze_qty_m,a.freeze_qty_s,a.replenishin_qty_m,a.replenishin_qty_s,a.replenishout_qty_m,a.replenishout_qty_s,a.movein_qty_m,a.movein_qty_s,a.moveout_qty_m,a.moveout_qty_s" +
                                " from " + main.g_space + ".inv_sku_location a "+
                                " LEFT JOIN WMS.mst_sku b on  a.inv_org = b.inv_org AND a.consignor = b.consignor AND a.sku= b.sku  "+
                                " LEFT JOIN WMS.mst_pack c on  a.inv_org = c.inv_org and a.consignor=c.consignor and a.pack_code = c.pack_code  "+
                                " LEFT JOIN WMS.mst_location m on  a.inv_org = m.inv_org AND a.location_code = m.location_code  "+
                                " LEFT JOIN WMS.mst_area n on m.inv_org = n.inv_org AND m.area_code = n.area_code  " +
                                " LEFT JOIN WMS.mst_warehouse mw on n.inv_org = mw.inv_org and n.warehouse = mw.warehouse " +
                                 " WHERE 1=1 and a.inv_org='" + main.g_org + "' ";
                               // " and a.stock_qty_m >0 and a.inv_org='" + main.g_org + "' ";



                // pallet 已不用
                str_sql_pallet = "select b.sku_name,b.spec,b.brand,b.main_unit,b.sub_unit,b.warehouseman,"+
                                "case b.standby_3 when '10' then '正常' when '13' then '禁销' when '20' then '预淘汰' when '30' then '淘汰' when '0' then '新品' else '' end lifecycle_status_name," +  //10-正常、13-禁销、20-预淘汰、30-淘汰、0-新品
                                "a.consignor,a.pallet_no,case a.location_code when 'QC001' then '待检区' else a.location_code end location_code,n.warehouse,a.sku sku,a.pack_code,a.stock_qty_m,a.stock_qty_s,a.ungot_qty_m,a.ungot_qty_s," +
                                 "a.allocate_qty_m,a.allocate_qty_s,a.replenishin_qty_m,a.replenishin_qty_s,a.replenishout_qty_m,a.replenishout_qty_s,a.movein_qty_m,a.movein_qty_s,a.moveout_qty_m,a.moveout_qty_s " +
                                 ",a.freeze_qty_m,a.freeze_qty_s,a.shelflife_day,a.order_price,a.order_unit,a.cubage,a.gross_weight,a.net_weight,isnull((select case c.spare_flag when '1' then round(a.stock_qty_m/isnull(c.qty05,1),0) else round(a.stock_qty_m/isnull((c.qty02 * c.qty03),1),0) end  from " + main.g_space + ".mst_pack c where a.inv_org = c.inv_org and a.consignor =c.consignor and a.sku = c.sku and a.pack_code = c.pack_code),0)  as box  from " + main.g_space + ".inv_sku_pallet a, " + main.g_space + ".mst_sku b  where a.inv_org = b.inv_org and a.consignor=b.consignor and a.sku=b.sku "+
                                " and a.inv_org='" + main.g_org + "' ";
                                // " and a.stock_qty_m >0 and a.inv_org='" + main.g_org + "' ";


                str_sql_Batch = "select RTRIM(b.inv_org) inv_org,RTRIM(b.batchno) batchno,RTRIM(a.consignor) consignor,RTRIM(a.sku) sku,RTRIM(d.sku_name) sku_name,RTRIM(d.barcode) barcode, RTRIM(d.standby_4)  as supply_code, " +
                       "(select RTRIM(class_name) class_name from wms.mst_stat_class sc where sc.class_code = d.class_code3 and sc.class_flag='C3'   ) as class_name,RTRIM(d.spec) spec,RTRIM(d.brand) brand," +
                       " case d.standby_3 when '10' then '正常' when '13' then '禁销' when '20' then '预淘汰' when '30' then '淘汰' when '0' then '新品' else '' end lifecycle_status_name," +  //10-正常、13-禁销、20-预淘汰、30-淘汰、0-新品
                       " RTRIM(a.pack_code) pack_code,c.unitsPerDelivery ," +
                       " ISNULL(CAST(c.qty02 AS INT), 0) as box_spec," +
                       " case a.location_code when 'QC001' then '待检区' else RTRIM(a.location_code) end location_code,RTRIM(n.warehouse) warehouse,"+
                       " ISNULL(CAST(sum(a.stock_qty_m) AS INT), 0) stock_qty_m, RTRIM(d.main_unit) main_unit, sum(a.stock_qty_s) stock_qty_s,RTRIM(d.sub_unit) sub_unit " +
                       "  ,ISNULL(CAST(sum(a.allocate_qty_m) AS INT), 0)  allocate_qty_m,sum(a.allocate_qty_s) allocate_qty_s,sum(a.freeze_qty_m) freeze_qty_m,sum(a.freeze_qty_s) freeze_qty_s " +
                       "  ,sum(a.cubage) cubage,sum(a.gross_weight) gross_weight,sum(a.net_weight) net_weight,"+
                       "   (select top 1 RTRIM(consignor_name) consignor_name from wms.mst_customer where inv_org=b.inv_org and supply_flag='1' and consignor = b.lot_attribute4 ) supply_name," +
                       "  RTRIM(b.lot_attribute1) lot_attribute1,RTRIM(b.lot_attribute2) lot_attribute2,RTRIM(b.lot_attribute3) lot_attribute3 " +
                       "  ,b.lot_attribute4,b.lot_attribute5,b.lot_attribute6,b.lot_attribute7,b.lot_attribute8,b.lot_attribute9 " +
                       "  ,b.lot_attribute10,b.lot_attribute11,b.lot_attribute12,b.lot_attribute13,b.lot_attribute14,b.lot_attribute15     "+ 

                       " FROM WMS.inv_sku_pallet a "+
                       " LEFT JOIN " + main.g_space + ".INV_SKU_BATCHNO b on a.inv_org= b.inv_org AND a.batchno= b.batchno  AND a.consignor= b.consignor AND a.sku= b.sku AND a.pack_code= b.pack_code   " +
                       " LEFT JOIN " + main.g_space + ".mst_sku d ON a.inv_org= d.inv_org AND a.consignor= d.consignor  AND a.sku= d.sku  " +
                       " LEFT JOIN " + main.g_space + ".mst_pack c ON a.inv_org = c.inv_org AND a.consignor = c.consignor AND a.sku = c.sku AND a.pack_code = c.pack_code  " +
                       " LEFT JOIN " + main.g_space + ".mst_location m ON a.inv_org = m.inv_org AND a.location_code = m.location_code  " +
                       " LEFT JOIN " + main.g_space + ".mst_area n ON m.inv_org = n.inv_org AND m.area_code     = n.area_code  " +
                       "   where 1=1  " +
                    //"   where a.stock_qty_m>0 and a.inv_org=b.inv_org and a.batchno=b.batchno and a.consignor=b.consignor and a.sku=b.sku and a.pack_code=b.pack_code " +
                       "   and a.inv_org='" + main.g_org + "' ";
            } 
            else          
            {
                ////str_sql_sku = "select b.sku_name,b.spec,b.main_unit,b.sub_unit,b.warehouseman,a.* ,nvl((select case c.spare_flag when '1' then round(a.stock_qty_m/c.qty05,0) else round(a.stock_qty_m/(c.qty02 * c.qty03),0) end  from " + main.g_space + ".mst_pack c where a.inv_org = c.inv_org and a.consignor =c.consignor and a.sku = c.sku and a.pack_code = c.pack_code),0)  as box  from " + main.g_space + ".inv_sku a left join wms.mst_sku b  on a.inv_org = b.inv_org and a.consignor=b.consignor and a.sku=b.sku  " ;
                //str_sql_location = "select b.sku_name,b.spec,b.brand,b.main_unit,b.sub_unit,b.warehouseman,a.consignor,a.sku sku,case a.location_code when 'QC001' then '待检区' else a.location_code end location_code,d.warehouse,a.pack_code,a.stock_qty_m,a.stock_qty_s,a.ungot_qty_m,a.ungot_qty_s," +
                //                "a.allocate_qty_m,a.allocate_qty_s,a.freeze_qty_m,a.freeze_qty_s,a.replenishin_qty_m,a.replenishin_qty_s,a.replenishout_qty_m,a.replenishout_qty_s,a.movein_qty_m,a.movein_qty_s,a.moveout_qty_m,a.moveout_qty_s" +
                //                " from " + main.g_space + ".inv_sku_location a, " + main.g_space + ".mst_sku b ," + main.g_space + ".mst_location c ," + main.g_space + ".mst_area d " +
                //                " where   a.inv_org = b.inv_org and a.consignor = b.consignor and  a.sku=b.sku " +
                //                " and a.inv_org = b.inv_org and a.location_code = c.location_code " +
                //                " and c.inv_org = d.inv_org and c.area_code     = d.area_code " +
                //                " and a.stock_qty_m >0 and a.inv_org='" + main.g_org + "' ";

                //str_sql_pallet = "select  b.sku_name,b.spec,b.brand,b.main_unit,b.sub_unit,b.warehouseman,a.consignor,a.pallet_no,case a.location_code when 'QC001' then '待检区' else a.location_code end location_code,a.sku sku,a.pack_code,a.stock_qty_m,a.stock_qty_s,a.ungot_qty_m,a.ungot_qty_s," +
                // "a.allocate_qty_m,a.allocate_qty_s,a.replenishin_qty_m,a.replenishin_qty_s,a.replenishout_qty_m,a.replenishout_qty_s,a.movein_qty_m,a.movein_qty_s,a.moveout_qty_m,a.moveout_qty_s " +           
                //    ",a.freeze_qty_m,a.freeze_qty_s,a.shelflife_day,a.order_price,a.order_unit,a.cubage,a.gross_weight,a.net_weight,nvl((select case c.spare_flag when '1' then round(a.stock_qty_m/c.qty05,0) else round(a.stock_qty_m/(c.qty02 * c.qty03),0) end  from " + main.g_space + ".mst_pack c where a.inv_org = c.inv_org and a.consignor =c.consignor and a.sku = c.sku and a.pack_code = c.pack_code),0)  as box  from " + main.g_space + ".inv_sku_pallet a," + main.g_space + ".mst_sku b  where a.inv_org = b.inv_org and a.consignor=b.consignor and a.sku=b.sku and a.stock_qty_m >0 and a.inv_org='" + main.g_org + "' ";

                //str_sql_Batch = "select b.batchno,a.consignor,a.sku sku,d.sku_name,d.spec,d.brand,a.pack_code,case a.location_code when 'QC001' then '待检区' else a.location_code end location_code,sum(a.stock_qty_m) stock_qty_m,d.main_unit, sum(a.stock_qty_s) stock_qty_s,d.sub_unit " +
                //      "  ,sum(a.allocate_qty_m) allocate_qty_m,sum(a.allocate_qty_s) allocate_qty_s,sum(a.freeze_qty_m) freeze_qty_m,sum(a.freeze_qty_s) freeze_qty_s " +
                //      "  ,sum(a.cubage) cubage,sum(a.gross_weight) gross_weight,sum(a.net_weight) net_weight,b.lot_attribute1,b.lot_attribute2,b.lot_attribute3 " +
                //      "  ,b.lot_attribute4,b.lot_attribute5,b.lot_attribute6,b.lot_attribute7,b.lot_attribute8,b.lot_attribute9 " +
                //      "  ,b.lot_attribute10,b.lot_attribute11,b.lot_attribute12,b.lot_attribute13,b.lot_attribute14,b.lot_attribute15      from " + main.g_space + ".inv_sku_pallet a ," + main.g_space + ".INV_SKU_BATCHNO b," + main.g_space + ".mst_sku d ," + main.g_space + ".mst_pack c" +
                //      "   where a.stock_qty_m>0 and a.inv_org=b.inv_org and a.batchno=b.batchno and a.consignor=b.consignor and a.sku=b.sku and a.pack_code=b.pack_code " +
                //      "   and a.inv_org=d.inv_org and a.consignor=d.consignor and a.sku=d.sku and a.inv_org='" + main.g_org + "' and a.inv_org = c.inv_org and a.consignor =c.consignor and a.sku = c.sku and a.pack_code = c.pack_code";
            }

            if (chkBox_inv.Checked) {
                str_sql_sku += " and a.stock_qty_m >0 ";
                str_sql_sku_fix += " and a.stock_qty_m >0 ";
                //str_sql_sku_location += " and a.stock_qty_m >0 ";
                str_sql_location += " and a.stock_qty_m >0 ";
                str_sql_pallet += " and a.stock_qty_m >0 ";
                str_sql_Batch += " and a.stock_qty_m >0 ";
            }

            if (chkBox_fix.Checked)
            {
                str_sql_sku += " and b.fix_flag = '1' ";
                //str_sql_sku_location += " and b.fix_flag = '1' and b.fix_location = m.location_code ";
                str_sql_sku_fix += " and b.fix_flag = '1' ";
                str_sql_location += " and b.fix_flag = '1' ";
                str_sql_pallet += " and b.fix_flag = '1' ";
                str_sql_Batch += " and d.fix_flag = '1'  ";
            }

            if (!chkBox_othersku.Checked)
            {

                str_sql_location += " and a.location_code not in (select location_code from wms.mst_location where kind_flag=1 and location_code <> b.fix_location )  ";
            }

            //判断货主知否合法
            if (tb_cusname.Text.Trim() == "" && cb_consignor.Text.Trim() != "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Owner does not exist or is not the owner's permission!", main.g_info_dataset) + "!");
                return;
            }
            //判断是否有货主
            if (cb_consignor.Text.Trim() != "")
            {
                str_sql_sku += " and b.consignor='" + cb_consignor.Text.Trim() + "' ";
                //str_sql_sku_location += " and a.consignor='" + cb_consignor.Text.Trim() + "' ";
                str_sql_sku_fix += " and a.consignor='" + cb_consignor.Text.Trim() + "' ";
                str_sql_location += " and a.consignor='" + cb_consignor.Text.Trim() + "' ";
                str_sql_pallet += " and a.consignor='" + cb_consignor.Text.Trim() + "' ";
                str_sql_Batch += " and a.consignor='" + cb_consignor.Text.Trim() + "' ";
            }
            //else
            //{
            //    str_sql_location += " and  a.consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            //                 " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            //    str_sql_pallet += " and  a.consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            //                 " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            //    str_sql_Batch += " and  a.consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            //                " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            //}
            //判断商品码
            if (tb_sku.Text.Trim() != "")
            {
                string[] skuList = tb_sku.Text.Trim().Split(',');
                string skuWhere = "";
                if (skuList.Length > 0)
                {
                    foreach (string item in skuList)
                    {
                        if (skuWhere == "")
                        {
                            skuWhere = "'" + item + "'";
                        }
                        else
                        {
                            skuWhere += ",'" + item + "'";
                        }
                    }
                    str_sql_sku         +=  " and b.sku in (" + skuWhere + ")";
                    str_sql_sku_fix += " and a.sku in (" + skuWhere + ")";
                    str_sql_location += " and a.sku in (" + skuWhere + ")";
                    str_sql_pallet      +=  " and a.sku in (" + skuWhere + ")";
                    str_sql_Batch       +=  " and a.sku in (" + skuWhere + ")";
                }

                //str_sql_location += " and a.sku = '" + tb_sku.Text + "' ";
                //str_sql_pallet += " and a.sku = '" + tb_sku.Text + "' ";
                //str_sql_Batch += " and a.sku = '" + tb_sku.Text + "' ";
            }
            //if (tb_sku.Text != "")
            //{
            //    str_sql_location += " and a.sku = '" + tb_sku.Text + "' ";
            //    str_sql_pallet += " and a.sku = '" + tb_sku.Text + "' ";
            //    str_sql_Batch += " and a.sku = '" + tb_sku.Text + "' ";
            //}
            if (tb_sku_name.Text != "")
            {
                str_sql_sku += " and b.sku_name like '%" + tb_sku_name.Text + "%' ";
                str_sql_sku_fix += " and b.sku_name like '%" + tb_sku_name.Text + "%' ";
                str_sql_location += " and b.sku_name like '%" + tb_sku_name.Text + "%' ";
                str_sql_pallet += " and b.sku_name like '%" + tb_sku_name.Text + "%' ";
                str_sql_Batch += " and d.sku_name like '%" + tb_sku_name.Text + "%' ";
            }
            if (tb_barcode.Text != "")
            {
                str_sql_sku += " and b.barcode like '%" + tb_barcode.Text + "%' ";
                str_sql_sku_fix += " and b.barcode like '%" + tb_barcode.Text + "%' ";
                str_sql_location += " and b.barcode like '%" + tb_barcode.Text + "%' ";
                str_sql_pallet += " and b.barcode like '%" + tb_barcode.Text + "%' ";
                str_sql_Batch += " and d.barcode like '%" + tb_barcode.Text + "%' ";
            }
            if (tb_supply_code.Text != "")
            {
                str_sql_sku += " and b.standby_4 = '" + tb_supply_code.Text + "' ";
                str_sql_sku_fix += " and b.standby_4 = '" + tb_supply_code.Text + "' ";
                str_sql_location += " and b.standby_4 = '" + tb_supply_code.Text + "' ";
                str_sql_pallet += " and b.standby_4 = '" + tb_supply_code.Text + "' ";
                str_sql_Batch += " and d.standby_4 = '" + tb_supply_code.Text + "' ";
            }
            if (tb_spec.Text.Trim() != "")
            {
                str_sql_sku += " and b.spec like '%" + tb_spec.Text + "%' ";
                str_sql_sku_fix += " and b.spec like '%" + tb_spec.Text + "%' ";
                str_sql_location += " and b.spec like '%" + tb_spec.Text + "%' ";
                str_sql_pallet += " and b.spec like '%" + tb_spec.Text + "%' ";
                str_sql_Batch += " and d.spec like '%" + tb_spec.Text + "%' ";
            }
            if (cbx_sku_class.Text.Trim() != "")
            {
                str_sql_sku += " and b.sku_class = '" + cbx_sku_class.SelectedValue.ToString().Trim() + "' ";
                str_sql_sku_fix += " and b.sku_class = '" + cbx_sku_class.SelectedValue.ToString().Trim() + "' ";
                str_sql_location += " and b.sku_class = '" + cbx_sku_class.SelectedValue.ToString().Trim() + "' ";
                str_sql_pallet += " and b.sku_class = '" + cbx_sku_class.SelectedValue.ToString().Trim() + "' ";
                str_sql_Batch += " and d.sku_class = '" + cbx_sku_class.SelectedValue.ToString().Trim() + "' ";
            }
            if (cbx_lifecycle_status.Text.Trim() != "")
            {
                str_sql_sku += " and b.standby_3 = '" + cbx_lifecycle_status.SelectedValue.ToString().Trim() + "' ";
                str_sql_sku_fix += " and b.standby_3 = '" + cbx_lifecycle_status.SelectedValue.ToString().Trim() + "' ";
                str_sql_location += " and b.standby_3 = '" + cbx_lifecycle_status.SelectedValue.ToString().Trim() + "' ";
                str_sql_pallet += " and b.standby_3 = '" + cbx_lifecycle_status.SelectedValue.ToString().Trim() + "' ";
                str_sql_Batch += " and d.standby_3 = '" + cbx_lifecycle_status.SelectedValue.ToString().Trim() + "' ";
            }
            if (tb_warehouseman.Text.Trim() != "")
            {
                str_sql_sku += " and b.warehouseman = '" + tb_warehouseman.Text + "' ";
                //str_sql_sku += " and a.sku in (  select t1.sku from wms.inv_sku_location t1 ,WMS.mst_location t2,WMS.mst_area t3  " +
                //    " where t1.location_code = t2.location_code and t2.area_code = t3.area_code and t3.warehouseman = '" + tb_warehouseman.Text + "') ";
                str_sql_sku_fix += " and b.warehouseman = '" + tb_warehouseman.Text + "' ";
                //str_sql_sku_fix += " and a.sku in (  select t1.sku from wms.inv_sku_location t1 ,WMS.mst_location t2,WMS.mst_area t3  " +
                //    " where t1.location_code = t2.location_code and t2.area_code = t3.area_code and t3.warehouseman = '" + tb_warehouseman.Text + "') "; ;
                str_sql_location += " and b.warehouseman = '" + tb_warehouseman.Text + "' ";
                str_sql_pallet += " and b.warehouseman = '" + tb_warehouseman.Text + "' ";
                str_sql_Batch += " and d.warehouseman = '" + tb_warehouseman.Text + "' ";
            }
            if (tb_warehouse.Text.Trim() != "")
            {
                str_sql_sku += " and n.warehouse = '" + tb_warehouse.Text + "' ";
                //str_sql_sku += " and a.sku in (  select t1.sku from wms.inv_sku_location t1 ,WMS.mst_location t2,WMS.mst_area t3  " +
                //    " where t1.location_code = t2.location_code and t2.area_code = t3.area_code and t3.warehouse = '" + tb_warehouse.Text + "') ";
                str_sql_sku_fix += " and n.warehouse = '" + tb_warehouse.Text + "' ";
                //str_sql_sku_fix += " and a.sku in (  select t1.sku from wms.inv_sku_location t1 ,WMS.mst_location t2,WMS.mst_area t3  " +
                //    " where t1.location_code = t2.location_code and t2.area_code = t3.area_code and t3.warehouse = '" + tb_warehouse.Text + "') ";

                str_sql_location        += " and n.warehouse = '" + tb_warehouse.Text + "' ";
                str_sql_pallet          += " and n.warehouse = '" + tb_warehouse.Text + "' ";
                str_sql_Batch           += " and n.warehouse = '" + tb_warehouse.Text + "' ";
            }
            //if (tb_warehouseman.Text.Trim() != "" || tb_warehouse.Text.Trim() != "")
            //{
            //    str_sql_sku += " and a.sku in (" + str_sql_sku_location + ")";
            //}

            if (tb_location_code.Text != "")
            {
                if (tb_location_code2.Text.Trim() != "")
                {
                    str_sql_sku += " and b.fix_location between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
                    str_sql_sku_fix += " and b.fix_location between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
                    //str_sql_sku_location += " and a.location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
                    str_sql_location += " and a.location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
                    str_sql_pallet += " and a.location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
                    str_sql_Batch += " and a.location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
                }
                else
                {
                    str_sql_sku += " and b.fix_location like '" + tb_location_code.Text + "%' ";
                    str_sql_sku_fix += " and b.fix_location like '" + tb_location_code.Text + "%' ";
                    //str_sql_sku_location += " and a.location_code like '" + tb_location_code.Text + "%' ";
                    str_sql_location += " and a.location_code like '" + tb_location_code.Text + "%' ";
                    str_sql_pallet += " and a.location_code like '" + tb_location_code.Text + "%' ";
                    str_sql_Batch += " and a.location_code like '" + tb_location_code.Text + "%' ";

                }
               
            }
            if (tb_pallet_no.Text != "")
            {
                str_sql_pallet += " and a.pallet_no = '" + tb_pallet_no.Text + "' ";
                str_sql_Batch += " and a.pallet_no = '" + tb_pallet_no.Text + "' ";
                for (int i = 0; i < tabControl1.TabPages.Count; i++)
                {
                    if (tabControl1.TabPages[i].Name == "tp_location_code")
                    {
                        tabControl1.TabPages.Remove(tp_location_code);
                        break;
                    }
                }
            }

            if (tb_asn_no.Text.Trim() != "")
            {
                for (int i = 0; i < tabControl1.TabPages.Count; i++)
                {
                    if (tabControl1.TabPages[i].Name == "tp_location_code")
                    {
                        tabControl1.TabPages.Remove(tp_location_code);
                        break;
                    }
                }
                str_sql_Batch += " and b.lot_attribute4 = '" + tb_asn_no.Text.Trim() + "' ";
                str_sql_pallet += " and exists (select 1 from " + main.g_space + ".INV_SKU_BATCHNO inv where inv.inv_org=a.inv_org and inv.consignor=a.consignor and inv.sku=a.sku and inv.batchno=a.batchno and inv.pack_code=a.pack_code and inv.lot_attribute4='" + tb_asn_no.Text.Trim() + "') ";
            }

            if (tb_Batch.Text.Trim() != "")
            {
                for (int i = 0; i < tabControl1.TabPages.Count; i++)
                {
                    if (tabControl1.TabPages[i].Name == "tp_location_code")
                    {
                        tabControl1.TabPages.Remove(tp_location_code);
                        break;
                    }
                }
                str_sql_Batch += " and b.lot_attribute5 = '" + tb_Batch.Text.Trim() + "' ";
                str_sql_pallet += " and exists (select 1 from " + main.g_space + ".INV_SKU_BATCHNO inv where inv.inv_org=a.inv_org and inv.consignor=a.consignor and inv.sku=a.sku and inv.batchno=a.batchno and inv.pack_code=a.pack_code and inv.lot_attribute5='" + tb_Batch.Text.Trim() + "') ";
            }
         
            //}  
            ds_sku = null;
            ds_sku_fix = null;
            ds_Batch = null;
            ds_location = null;

            str_sql_sku += " GROUP BY " +
                            " n.warehouse, " +
                            " mw.warehouse_name, " +
                            " b.sku, " +
                            " b.sku_name, " +
                            " b.barcode, " +
                            " b.standby_4, " +
                            " b.fix_location, " +
                            " b.class_code3, " +
                            " b.standby_3 , " +
                            " c.pack_code, " +
                            " c.unitsPerDelivery , " +
                            " c.qty02, " +
                            " b.spec, " +
                            " b.main_unit, " +
                            " b.sub_unit, " +
                            " b.warehouseman ";

            str_sql_Batch += "   group by b.inv_org,b.batchno,a.consignor,a.sku,d.sku_name,d.class_code3,d.barcode,d.standby_4,d.spec,d.brand,d.standby_3, a.pack_code,c.unitsPerDelivery ,c.qty02 ,a.location_code,n.warehouse,d.main_unit,d.sub_unit " +
                           "  ,b.lot_attribute1,b.lot_attribute2,b.lot_attribute3 " +
                           "  ,b.lot_attribute4,b.lot_attribute5,b.lot_attribute6,b.lot_attribute7,b.lot_attribute8,b.lot_attribute9 " +
                           "  ,b.lot_attribute10,b.lot_attribute11,b.lot_attribute12,b.lot_attribute13,b.lot_attribute14,b.lot_attribute15,c.spare_flag ";
            
            //////增加收货确认未上架确认的库存
            ////if (g_str_putaway == "1")
            ////{
            ////    if (main.dbtype == "SQL")
            ////    {
            ////        str_sql_location = str_sql_location + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,sku,'待存区' location_code,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////                        "sum(allocate_qty_m) allocate_qty_m,sum(allocate_qty_s) allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,sum(replenishin_qty_m) replenishin_qty_m,sum(replenishin_qty_s) replenishin_qty_s,sum(replenishout_qty_m) replenishout_qty_m," +
            ////                        "sum(replenishout_qty_s) replenishout_qty_s,sum(movein_qty_m) movein_qty_m,sum(movein_qty_s) movein_qty_s,sum(moveout_qty_m) moveout_qty_m,sum(moveout_qty_s) moveout_qty_s from " + main.g_space + ".VIEW_SQL_INV_NOTPUTAWAY_LOC where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_pallet = str_sql_pallet + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,pallet_no,'待存区' location_code,sku,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////            "0 allocate_qty_m,0 allocate_qty_s,0 replenishin_qty_m,0 replenishin_qty_s,0 replenishout_qty_m,0 replenishout_qty_s,0 movein_qty_m,0 movein_qty_s,0 moveout_qty_m,0 moveout_qty_s " +
            ////                         ",sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,shelflife_day,order_price,order_unit,sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight  from " + main.g_space + ".VIEW_SQL_INV_NOTPUTAWAY_PALLET where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_Batch = str_sql_Batch + " union all select batchno,consignor,sku,sku_name,spec,'' brand,pack_code,'待存区' location_code,sum(stock_qty_m) stock_qty_m,main_unit,sum(stock_qty_s) stock_qty_s,sub_unit " +
            ////               ",0 allocate_qty_m,0 allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s " +
            ////               ",sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight,lot_attribute1,lot_attribute2,lot_attribute3 " +
            ////               ",lot_attribute4,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8,lot_attribute9 " +
            ////               ",lot_attribute10,lot_attribute11,lot_attribute12,lot_attribute13,lot_attribute14,lot_attribute15  from " + main.g_space + ".VIEW_SQL_INV_NOTPUTAWAY_BATCH where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////    }
            ////    else
            ////    {
            ////        str_sql_location = str_sql_location + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,sku,'待存区' location_code,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////                        "sum(allocate_qty_m) allocate_qty_m,sum(allocate_qty_s) allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,sum(replenishin_qty_m) replenishin_qty_m,sum(replenishin_qty_s) replenishin_qty_s,sum(replenishout_qty_m) replenishout_qty_m," +
            ////                        "sum(replenishout_qty_s) replenishout_qty_s,sum(movein_qty_m) movein_qty_m,sum(movein_qty_s) movein_qty_s,sum(moveout_qty_m) moveout_qty_m,sum(moveout_qty_s) moveout_qty_s from " + main.g_space + ".VIEW_ORL_INV_NOTPUTAWAY_LOC where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_pallet = str_sql_pallet + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,pallet_no,'待存区' location_code,sku,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////            "0 allocate_qty_m,0 allocate_qty_s,0 replenishin_qty_m,0 replenishin_qty_s,0 replenishout_qty_m,0 replenishout_qty_s,0 movein_qty_m,0 movein_qty_s,0 moveout_qty_m,0 moveout_qty_s " +
            ////                         ",sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,shelflife_day,order_price,order_unit,sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight   from " + main.g_space + ".VIEW_ORL_INV_NOTPUTAWAY_PALLET where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_Batch = str_sql_Batch + " union all select batchno,consignor,sku,sku_name,spec,'' brand,pack_code,'待存区' location_code,sum(stock_qty_m) stock_qty_m,main_unit,sum(stock_qty_s) stock_qty_s,sub_unit " +
            ////               ",0 allocate_qty_m,0 allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s " + 
            ////               ",sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight,lot_attribute1,lot_attribute2,lot_attribute3 " +
            ////               ",lot_attribute4,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8,lot_attribute9 " +
            ////               ",lot_attribute10,lot_attribute11,lot_attribute12,lot_attribute13,lot_attribute14,lot_attribute15  from " + main.g_space + ".VIEW_ORL_INV_NOTPUTAWAY_BATCH where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";
            ////    }

            ////    //判断是否有货主
            ////    if (cb_consignor.Text.Trim() != "")
            ////    {
            ////        str_sql_location += " and consignor='" + cb_consignor.Text.Trim() + "' ";
            ////        str_sql_pallet += " and consignor='" + cb_consignor.Text.Trim() + "' ";
            ////        str_sql_Batch += " and consignor='" + cb_consignor.Text.Trim() + "' ";
            ////    }
            ////    //else
            ////    //{
            ////    //    str_sql_location += " and  consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            ////    //                 " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            ////    //    str_sql_pallet += " and  consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            ////    //                 " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            ////    //    str_sql_Batch += " and  consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            ////    //                " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            ////    //}
            ////    if (tb_sku.Text != "")
            ////    {
            ////        str_sql_location += " and sku = '" + tb_sku.Text + "' ";
            ////        str_sql_pallet += " and sku = '" + tb_sku.Text + "' ";
            ////        str_sql_Batch += " and sku = '" + tb_sku.Text + "' ";
            ////    }
            ////    if (tb_sku_name.Text != "")
            ////    {
            ////        str_sql_location += " and sku_name like '%" + tb_sku_name.Text + "%' ";
            ////        str_sql_pallet += " and sku_name like '%" + tb_sku_name.Text + "%' ";
            ////        str_sql_Batch += " and sku_name like '%" + tb_sku_name.Text + "%' ";
            ////    }
            ////    if (tb_spec.Text.Trim() != "")
            ////    {
            ////        str_sql_location += " and spec like '%" + tb_spec.Text + "%' ";
            ////        str_sql_pallet += " and spec like '%" + tb_spec.Text + "%' ";
            ////        str_sql_Batch += " and spec like '%" + tb_spec.Text + "%' ";
            ////    }
            ////    if (tb_warehouseman.Text.Trim() != "")
            ////    {
            ////        str_sql_location += " and warehouseman = '" + tb_warehouseman.Text + "' ";
            ////        str_sql_pallet += " and warehouseman = '" + tb_warehouseman.Text + "' ";
            ////        str_sql_Batch += " and warehouseman = '" + tb_warehouseman.Text + "' ";
            ////    }

            ////    if (tb_location_code.Text != "")
            ////    {                  

            ////        if (tb_location_code2.Text.Trim() != "")
            ////        {
            ////            str_sql_location += " and location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
            ////            str_sql_pallet += " and location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
            ////            str_sql_Batch += " and location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
            ////        }
            ////        else
            ////        {
            ////            str_sql_location += " and location_code like '" + tb_location_code.Text + "%' ";
            ////            str_sql_pallet += " and location_code like '" + tb_location_code.Text + "%' ";
            ////            str_sql_Batch += " and location_code like '" + tb_location_code.Text + "%' ";
            ////        }
            ////    }

            ////    str_sql_location += " group by sku_name,spec,main_unit,sub_unit,warehouseman,consignor,sku,location_code,pack_code";
            ////    str_sql_pallet += " group by sku_name,spec,main_unit,sub_unit,warehouseman,consignor,pallet_no,location_code,sku,pack_code,shelflife_day,order_price,order_unit";
            ////    str_sql_Batch += " group by batchno,consignor,sku,sku_name,spec,pack_code,location_code,main_unit,sub_unit,lot_attribute1,lot_attribute2,lot_attribute3 " +
            ////               ",lot_attribute4,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8,lot_attribute9 " +
            ////               ",lot_attribute10,lot_attribute11,lot_attribute12,lot_attribute13,lot_attribute14,lot_attribute15";
            ////}

            //////增加拣货确认未发货确人的库存
            ////if (g_str_ship == "1")
            ////{
            ////    if (main.dbtype == "SQL")
            ////    {
            ////        str_sql_location = str_sql_location + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,sku,location_code,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////                        "sum(allocate_qty_m) allocate_qty_m,sum(allocate_qty_s) allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,sum(replenishin_qty_m) replenishin_qty_m,sum(replenishin_qty_s) replenishin_qty_s,sum(replenishout_qty_m) replenishout_qty_m," +
            ////                        "sum(replenishout_qty_s) replenishout_qty_s,sum(movein_qty_m) movein_qty_m,sum(movein_qty_s) movein_qty_s,sum(moveout_qty_m) moveout_qty_m,sum(moveout_qty_s) moveout_qty_s from " + main.g_space + ".VIEW_SQL_INV_NOTSHIP_LOC where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_pallet = str_sql_pallet + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,pallet_no,location_code,sku,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////            "0 allocate_qty_m,0 allocate_qty_s,0 replenishin_qty_m,0 replenishin_qty_s,0 replenishout_qty_m,0 replenishout_qty_s,0 movein_qty_m,0 movein_qty_s,0 moveout_qty_m,0 moveout_qty_s " +
            ////                         ",sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,shelflife_day,order_price,order_unit,sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight  from " + main.g_space + ".VIEW_SQL_INV_NOTSHIP_PALLET where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_Batch = str_sql_Batch + " union all select batchno,consignor,sku,sku_name,spec,'' brand,pack_code,location_code,sum(stock_qty_m) stock_qty_m,main_unit,sum(stock_qty_s) stock_qty_s,sub_unit " +
            ////               ",0 allocate_qty_m,0 allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s " +
            ////               ",sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight,lot_attribute1,lot_attribute2,lot_attribute3 " +
            ////               ",lot_attribute4,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8,lot_attribute9 " +
            ////               ",lot_attribute10,lot_attribute11,lot_attribute12,lot_attribute13,lot_attribute14,lot_attribute15  from " + main.g_space + ".VIEW_SQL_INV_NOTSHIP_BATCH where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////    }
            ////    else 
            ////    {
            ////        str_sql_location = str_sql_location + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,sku,location_code,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////                        "sum(allocate_qty_m) allocate_qty_m,sum(allocate_qty_s) allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,sum(replenishin_qty_m) replenishin_qty_m,sum(replenishin_qty_s) replenishin_qty_s,sum(replenishout_qty_m) replenishout_qty_m," +
            ////                        "sum(replenishout_qty_s) replenishout_qty_s,sum(movein_qty_m) movein_qty_m,sum(movein_qty_s) movein_qty_s,sum(moveout_qty_m) moveout_qty_m,sum(moveout_qty_s) moveout_qty_s  from " + main.g_space + ".VIEW_ORL_INV_NOTSHIP_LOC where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_pallet = str_sql_pallet + " union all select sku_name,spec,'' brand,main_unit,sub_unit,warehouseman,consignor,pallet_no,location_code,sku,pack_code,sum(stock_qty_m) stock_qty_m,sum(stock_qty_s) stock_qty_s,sum(ungot_qty_m) ungot_qty_m,sum(ungot_qty_s) ungot_qty_s," +
            ////            "0 allocate_qty_m,0 allocate_qty_s,0 replenishin_qty_m,0 replenishin_qty_s,0 replenishout_qty_m,0 replenishout_qty_s,0 movein_qty_m,0 movein_qty_s,0 moveout_qty_m,0 moveout_qty_s " +
            ////                         ",sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s,shelflife_day,order_price,order_unit,sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight  from " + main.g_space + ".VIEW_ORL_INV_NOTSHIP_PALLET where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";

            ////        str_sql_Batch = str_sql_Batch + " union all select batchno,consignor,sku,sku_name,spec,'' brand,pack_code,location_code,sum(stock_qty_m) stock_qty_m,main_unit,sum(stock_qty_s) stock_qty_s,sub_unit " +
            ////               ",0 allocate_qty_m,0 allocate_qty_s,sum(freeze_qty_m) freeze_qty_m,sum(freeze_qty_s) freeze_qty_s " +
            ////               ",sum(cubage) cubage,sum(gross_weight) gross_weight,sum(net_weight) net_weight,lot_attribute1,lot_attribute2,lot_attribute3 " +
            ////               ",lot_attribute4,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8,lot_attribute9 " +
            ////               ",lot_attribute10,lot_attribute11,lot_attribute12,lot_attribute13,lot_attribute14,lot_attribute15  from " + main.g_space + ".VIEW_ORL_INV_NOTSHIP_BATCH where stock_qty_m > 0 and inv_org='" + main.g_org + "' ";
            ////    }

            ////    //判断是否有货主
            ////    if (cb_consignor.Text.Trim() != "")
            ////    {
            ////        str_sql_location += " and consignor='" + cb_consignor.Text.Trim() + "' ";
            ////        str_sql_pallet += " and consignor='" + cb_consignor.Text.Trim() + "' ";
            ////        str_sql_Batch += " and consignor='" + cb_consignor.Text.Trim() + "' ";
            ////    }
            ////    //else
            ////    //{
            ////    //    str_sql_location += " and  consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            ////    //                 " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            ////    //    str_sql_pallet += " and  consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            ////    //                 " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            ////    //    str_sql_Batch += " and  consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
            ////    //                " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            ////    //}
            ////    if (tb_sku.Text != "")
            ////    {
            ////        str_sql_location += " and sku = '" + tb_sku.Text + "' ";
            ////        str_sql_pallet += " and sku = '" + tb_sku.Text + "' ";
            ////        str_sql_Batch += " and sku = '" + tb_sku.Text + "' ";
            ////    }
            ////    if (tb_sku_name.Text != "")
            ////    {
            ////        str_sql_location += " and sku_name like '%" + tb_sku_name.Text + "%' ";
            ////        str_sql_pallet += " and sku_name like '%" + tb_sku_name.Text + "%' ";
            ////        str_sql_Batch += " and sku_name like '%" + tb_sku_name.Text + "%' ";
            ////    }
            ////    if (tb_spec.Text.Trim() != "")
            ////    {
            ////        str_sql_location += " and spec like '%" + tb_spec.Text + "%' ";
            ////        str_sql_pallet += " and spec like '%" + tb_spec.Text + "%' ";
            ////        str_sql_Batch += " and spec like '%" + tb_spec.Text + "%' ";
            ////    }
            ////    if (tb_warehouseman.Text.Trim() != "")
            ////    {
            ////        str_sql_location += " and warehouseman = '" + tb_warehouseman.Text + "' ";
            ////        str_sql_pallet += " and warehouseman = '" + tb_warehouseman.Text + "' ";
            ////        str_sql_Batch += " and warehouseman = '" + tb_warehouseman.Text + "' ";
            ////    }

            ////    if (tb_location_code.Text != "")
            ////    {                   
            ////        if (tb_location_code2.Text.Trim() != "")
            ////        {
            ////            str_sql_location += " and location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
            ////            str_sql_pallet += " and location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
            ////            str_sql_Batch += " and location_code between '" + tb_location_code.Text + "' and '" + tb_location_code2.Text.Trim() + "'";
            ////        }
            ////        else
            ////        {
            ////            str_sql_location += " and location_code like '" + tb_location_code.Text + "%' ";
            ////            str_sql_pallet += " and location_code like '" + tb_location_code.Text + "%' ";
            ////            str_sql_Batch += " and location_code like '" + tb_location_code.Text + "%' ";
            ////        }
            ////    }

            ////    str_sql_location += " group by sku_name,spec,main_unit,sub_unit,warehouseman,consignor,sku,location_code,pack_code";
            ////    str_sql_pallet += " group by sku_name,spec,main_unit,sub_unit,warehouseman,consignor,pallet_no,location_code,sku,pack_code,shelflife_day,order_price,order_unit";
            ////    str_sql_Batch += " group by batchno,consignor,sku,sku_name,spec,pack_code,location_code,main_unit,sub_unit,lot_attribute1,lot_attribute2,lot_attribute3 " +
            ////               ",lot_attribute4,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8,lot_attribute9 " +
            ////               ",lot_attribute10,lot_attribute11,lot_attribute12,lot_attribute13,lot_attribute14,lot_attribute15";
            ////}

            str_sql_sku += " order by sku,n.warehouse ";
            str_sql_sku_fix += " order by sku";
            str_sql_location += " order by sku";
            str_sql_pallet += " order by sku";
            str_sql_Batch += " order by sku";
            try  
            { 
                if (main.dbtype == "SQL")
                {

                    ds_sku = main.dbservice.Sql_exec_selectsql(str_sql_sku);
                    ds_sku_fix = main.dbservice.Sql_exec_selectsql(str_sql_sku_fix);
                    ds_Batch = main.dbservice.Sql_exec_selectsql(str_sql_Batch);
                    ds_location = main.dbservice.Sql_exec_selectsql(str_sql_location);
                    //ds_pallet = main.dbservice.Sql_exec_selectsql(str_sql_pallet);  
                }
                else
                {  

                    ds_Batch = main.dbservice.oracle_exec_selectsql(str_sql_Batch);
                    ds_location = main.dbservice.oracle_exec_selectsql(str_sql_location);
                    //ds_pallet = main.dbservice.oracle_exec_selectsql(str_sql_pallet);
                }
                //加载数据
                if (ds_Batch != null)
                {
                    this.dgv_batchno.DataSource = ds_Batch.Tables[0];
                    b_putaway_sort = true;
                    if (ds_Batch.Tables[0].Rows.Count > 0) //说明有数据,增加合计
                    {
                        //f_add_sku();
                        f_add_batchno();
                    }
                }
                if (ds_location != null)
                {
                    this.dvg_location_code.DataSource = ds_location.Tables[0];
                    b_putaway_sort = true;
                    if (ds_location.Tables[0].Rows.Count > 0)//说明有数据,增加合计
                    {
                        f_add_location();
                    }
                }

                if (ds_sku != null)
                {
                    this.dgv_sku.DataSource = ds_sku.Tables[0];
                    b_putaway_sort = true;
                    if (ds_sku.Tables[0].Rows.Count > 0) //说明有数据,增加合计
                    {
                        f_add_sku();
                    }
                }

                if (ds_sku_fix != null)
                {
                    this.dgv_sku_fix.DataSource = ds_sku_fix.Tables[0];
                    b_putaway_sort = true;
                    if (ds_sku_fix.Tables[0].Rows.Count > 0) //说明有数据,增加合计
                    {
                        f_add_sku_fix();
                    }
                }

                //if (ds_pallet != null)
                //{
                //    this.dvg_pallet.DataSource = ds_pallet.Tables[0];
                //    b_putaway_sort = true;
                //    if (ds_pallet.Tables[0].Rows.Count > 0) //说明有数据,增加合计
                //    {
                //        f_add_pallet();
                //    }
                //}
            }
            catch (System.Exception ee) 
            {

                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }

        }

        private void dvg_pallet_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1)//当前行为数据行
            //{
            //    string str_where = " where  a.consignor='" + dvg_pallet.Rows[e.RowIndex].Cells["consignor_2"].Value.ToString().Trim() + "' and a.sku='" + dvg_pallet.Rows[e.RowIndex].Cells["sku_2"].Value.ToString().Trim() + "'";
            //    get_batchon(str_where, dvg_pallet.Rows[e.RowIndex].Cells["sku_2"].Value.ToString());
            //}
        }

        private void dgv_sku_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1)//当前行为数据行
            //{
            //    string str_where = " where  a.inv_org = '" + main.g_org + "' and a.consignor='" + dgv_sku.Rows[e.RowIndex].Cells["consignor"].Value.ToString().Trim() + "' and a.sku='" + dgv_sku.Rows[e.RowIndex].Cells["sku"].Value.ToString().Trim() + "'";
            //    //get_batchon(str_where, dgv_sku.Rows[e.RowIndex].Cells["sku"].Value.ToString());
            //}
        }
        //获取批号
        private void get_batchon(string where, string sku)
        {
            //tabControl1.TabPages.Remove(tp_batchno);
            //tabControl1.TabPages.Add(tp_batchno);

            string str_sql_batchno = "select a.* ,(select case c.spare_flag when '1' then round(a.stock_qty_m/c.qty05,0) else round(a.stock_qty_m/(c.qty02 * c.qty03),0) end  from " + main.g_space + ".mst_pack c where a.inv_org = c.inv_org and a.consignor =c.consignor and a.sku = c.sku and a.pack_code = c.pack_code)  as box,b.sub_unit,b.main_unit,b.sku_name,b.spec  from " + main.g_space + ".inv_sku_batchno a left join wms.mst_sku b  on a.inv_org = b.inv_org and a.consignor=b.consignor and a.sku=b.sku " + where;
            string str_sql_attribute = "select lot01label, lot02label, lot03label, lot04label, lot05label, lot06label, lot07label, lot08label, lot09label, lot10label, " +
                                       "lot11label, lot12label, lot13label, lot14label, lot15label, lot01flag, lot02flag, lot03flag, lot04flag, lot05flag, lot06flag, lot07flag," +
                                       "lot08flag, lot09flag, lot10flag, lot11flag, lot12flag, lot13flag, lot14flag, lot15flag from " + main.g_space + ".mst_attribute mst_attribute left join " + main.g_space + ".mst_sku c on c.inv_org = mst_attribute.inv_org and c.lotno=mst_attribute.lotno where c.sku='" + sku.Trim() + "' and c.inv_org = '" + main.g_org + "'";

            DataSet ds_attribute;
            DataSet ds_batchno;
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_batchno = main.dbservice.Sql_exec_selectsql(str_sql_batchno);
                    ds_attribute = main.dbservice.Sql_exec_selectsql(str_sql_attribute);
                }
                else
                {
                    ds_batchno = main.dbservice.oracle_exec_selectsql(str_sql_batchno);
                    ds_attribute = main.dbservice.oracle_exec_selectsql(str_sql_attribute);
                }

                this.dgv_batchno.DataSource = ds_batchno.Tables[0];
                b_putaway_sort = true;
                if (ds_batchno.Tables[0].Rows.Count > 0) //说明有数据,增加合计
                {
                    f_add_batchno();
                }

                if (ds_attribute.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < 15; i++)
                    {
                        string Column = "lot_attribute" + (i + 1);
                        if (ds_attribute.Tables[0].Rows[0][i + 15].ToString().Trim() == "0")//判断属性是否显示
                        {

                            dgv_batchno.Columns[Column].Visible = false;
                        }
                        else
                        {
                            dgv_batchno.Columns[Column].HeaderText = ds_attribute.Tables[0].Rows[0][i].ToString().Trim();
                            if (ds_batchno.Tables[0].Rows.Count > 0)
                            {
                                for (int k = 0; k < ds_batchno.Tables[0].Rows.Count; k++)
                                {
                                    dgv_batchno.Rows[k].Cells[Column].Value = ds_batchno.Tables[0].Rows[k][Column].ToString().Trim();

                                }
                            }
                        }

                    }


                }

                this.tabControl1.SelectedIndex = 3;
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of lotno", main.g_info_dataset) + "!" + ee.ToString());
            }

        }



        private void dvg_pallet_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1)//当前行为数据行
            //{
            //    string str_where = " where  a.consignor='" + dvg_pallet.Rows[e.RowIndex].Cells["consignor_2"].Value.ToString().Trim() + "' and a.sku='" + dvg_pallet.Rows[e.RowIndex].Cells["sku_2"].Value.ToString().Trim() + "'";
            //    get_batchon(str_where, dvg_pallet.Rows[e.RowIndex].Cells["sku_2"].Value.ToString());
            //}
        }

        private void cb_consignor_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cb_consignor.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
            //SKU_CLASS          
            f_initskuclass(str_cust);
            // 生命周期
            f_init_lifecycle_status(str_cust);
        }
        //初始化商品生命周期
        private void f_init_lifecycle_status(string str_tmp)
        {
            DataSet f_ds_ls;

            //初始化待退库区
            try
            {
                f_ds_ls = main.g_class.comm_get_dictionary("lifecycle_status");
                //进行语言转换
                int i_ds;
                i_ds = f_ds_ls.Tables[0].Rows.Count;
                if (i_ds > 0)
                {
                    //f_ds.Tables[0].Rows.Remove(f_ds.Tables[0].Select("dictionary_code=0004")[0]);
                    for (int i = 0; i < f_ds_ls.Tables[0].Rows.Count; i++)
                    {
                        f_ds_ls.Tables[0].Rows[i]["lifecycle_status"] = main.g_class.comm_getchangestring(f_ds_ls.Tables[0].Rows[i]["lifecycle_status"].ToString().Trim(), main.g_info_dataset);
                    }
                }
                DataSet newf_ds = f_ds_ls.Copy();
                DataRow drnew;
                drnew = f_ds_ls.Tables[0].NewRow();
                drnew["lifecycle_status"] = "";
                f_ds_ls.Tables[0].Rows.Add(drnew);
                cbx_lifecycle_status.ValueMember = "dictionary_code";
                cbx_lifecycle_status.DisplayMember = "lifecycle_status";
                cbx_lifecycle_status.DataSource = f_ds_ls.Tables[0];
                cbx_lifecycle_status.Text = "";

            }
            catch (System.Exception ee)
            {
                MessageBox.Show("获取生命周期类型失败！");
                return;
            }



            
        }
        //初始化商品分类
        private void f_initskuclass(string str_tmp)
        {
            DataSet f_dsku;
         
            string str_temp;
            //首先清空
            cbx_sku_class.Text = "";
            //然后加载
            str_temp = "select sku_class,class_description from " + main.g_space + ".mst_sku_class where inv_org = '" + main.g_org + "' and consignor='" + str_tmp + "' order by sku_class";
            try
            {
                if (main.dbtype == "SQL")
                    f_dsku = main.dbservice.Sql_exec_selectsql(str_temp);
                else
                    f_dsku = main.dbservice.oracle_exec_selectsql(str_temp);
                cbx_sku_class.DataSource = f_dsku.Tables[0].DefaultView;
                cbx_sku_class.ValueMember = "sku_class";
                cbx_sku_class.DisplayMember = "class_description";
                cbx_sku_class.Text = "";
             

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of sku class", main.g_info_dataset) + "!" + ee.ToString());
            }
        }
        private void btn_sku_Click(object sender, EventArgs e)
        {
            //string str_customer;
            //w_single wsingle = new w_single();
            //str_customer = "";
            //w_single.str_qcustomer = str_customer;
            //wsingle.ShowDialog();
            //this.tb_sku.Text = w_single.str_qsku;

            w_query_batch wsingle = new w_query_batch();
            wsingle.ShowDialog();
            this.tb_sku.Text = w_query_batch.str_qcontent;

        }

        private void dvg_location_code_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1)//当前行为数据行
            //{
            //    string str_where = " where a.inv_org = '" + main.g_org + "' and a.consignor='" + dvg_location_code.Rows[e.RowIndex].Cells["consignor_1"].Value.ToString().Trim() + "' and a.sku='" + dvg_location_code.Rows[e.RowIndex].Cells["sku_1"].Value.ToString().Trim() + "'";
            //    get_batchon(str_where, dvg_location_code.Rows[e.RowIndex].Cells["sku_1"].Value.ToString());
            //}
            if (dvg_location_code.CurrentRow != null)
            {
                if (dvg_location_code.Columns[e.ColumnIndex].Name.ToString().Trim() == "allocate_qty_m2")
                {
                    //if (Convert.ToDecimal(dvg_location_code.Rows[e.RowIndex].Cells["allocate_qty_m2"].Value.ToString().Trim()) <= 0)
                    //{
                    //    return;
                    //}
                    string str_con = dvg_location_code.Rows[e.RowIndex].Cells["consignor_1"].Value.ToString().Trim();
                    string str_sku = dvg_location_code.Rows[e.RowIndex].Cells["sku_1"].Value.ToString().Trim();
                    string str_pack = dvg_location_code.Rows[e.RowIndex].Cells["pack_code_1"].Value.ToString().Trim();
                    string str_sql;
                    try
                    {
                        DataSet ds_temp;
                        if (main.dbtype == "SQL")
                        {
                            str_sql = "select c.wave_no,a.so_no,a.customer_sale_no,a.customer_name,round(isnull(b.allocate_qty_m,0)-isnull(b.pick_qty_m,0),4) qty" +
                                      ",a.address,a.standby_13   from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b,"+main.g_space+".so_ship_wavedetail c " +
                                       "  where a.inv_org=b.inv_org and a.consignor=b.consignor and a.so_no=b.so_no " +
                                        "  and round(isnull(b.allocate_qty_m,0) - isnull(b.pick_qty_m,0),4) > 0 and a.inv_org='" + main.g_org + "' and a.consignor='" + str_con + "'";
                            str_sql += " and a.inv_org*=c.inv_org and a.consignor*=c.consignor and a.so_no*=c.so_no ";
                            str_sql += " and b.sku='" + str_sku + "' and b.pack_code='" + str_pack + "' and a.so_flag='0' ";
                            str_sql += " and convert(varchar(10),a.prepare_time,102) = Convert(char(10),getdate(),102) "; // add
                            str_sql += " order by a.so_no ";
                            ds_temp = main.dbservice.Sql_exec_selectsql(str_sql);
                        }
                        else
                        {
                            str_sql = "select c.wave_no,a.so_no,a.customer_sale_no,a.customer_name,round(nvl(b.plan_so_qty_m,0)-nvl(b.pick_qty_m,0),4) qty" +
                                      ",a.address,a.standby_13   from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b ," + main.g_space + ".so_ship_wavedetail c" +
                                       "  where a.inv_org=b.inv_org and a.consignor=b.consignor and a.so_no=b.so_no " +
                                        "  and round(nvl(b.allocate_qty_m,0) - nvl(b.pick_qty_m,0),4) > 0 and a.inv_org='" + main.g_org + "' and a.consignor='" + str_con + "'";
                            str_sql += " and a.inv_org=c.inv_org(+) and a.consignor=c.consignor(+) and a.so_no=c.so_no(+) ";
                            str_sql += " and b.sku='" + str_sku + "' and b.pack_code='" + str_pack + "' and a.so_flag='0' order by a.so_no";
                            ds_temp = main.dbservice.oracle_exec_selectsql(str_sql);
                        }
                        if (ds_temp.Tables[0].Rows.Count > 0)
                        {
                            dgv_so.DataSource = ds_temp.Tables[0];
                            pl4.Visible = true;
                            //dgv_so.Focus();
                        }
                    }
                    catch (System.Exception ee)
                    {
                        MessageBox.Show("查询失败 " + ee.Message);
                        return;
                    }
                }
            }
        }
        //导出excel
        private void button1_Click(object sender, EventArgs e)
        {
            string str_tabtext = main.g_class.comm_getconversestring(tabControl1.SelectedTab.Text.Trim(), main.g_win_dataset).ToUpper();
            if (str_tabtext.Trim() == "SKU DETAIL")
            {
                main.g_class.DataGridViewToExcel(dgv_sku);
            }
            if (str_tabtext.Trim() == "商品库存")
            {
                main.g_class.DataGridViewToExcel(dgv_sku_fix);
            }
            if (str_tabtext.Trim() == "BATCH DETAIL")
            {
                main.g_class.DataGridViewToExcel(dgv_batchno);
            }
            if (str_tabtext.Trim() == "LOCATION DETAIL")
            {
                main.g_class.DataGridViewToExcel(dvg_location_code);
            }
            if (str_tabtext.Trim() == "PALLET DETAIL")
            {
                main.g_class.DataGridViewToExcel(dvg_pallet);
            }
        }

        private void dgv_sku_Sorted(object sender, EventArgs e)
        {
            string str_headertext;
            //先删除合计行
            ((DataTable)dgv_sku.DataSource).Rows.Remove(((DataTable)dgv_sku.DataSource).Select("sku = '合计'")[0]);
            //然后对Tables[0]重新排序
            DataTable dt;
            DataView dv = ((DataTable)dgv_sku.DataSource).DefaultView;
            //得到点击的列
            str_headertext = dgv_sku.Columns[i_sku_header].DataPropertyName.ToString().Trim();
            if (b_putaway_sort) //升序
            {
                dv.Sort = str_headertext + " DESC";
                b_putaway_sort = false;
            }
            else
            {
                dv.Sort = str_headertext;
                b_putaway_sort = true;
            }
            dt = dv.ToTable();
            dgv_sku.DataSource = null;
            dgv_sku.DataSource = dt;
            //增加合计行
            f_add_sku();

        }

        private void dgv_sku_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i_sku_header = e.ColumnIndex;
        }


        private void dgv_sku_fix_Sorted(object sender, EventArgs e)
        {
            string str_headertext;
            //先删除合计行
            ((DataTable)dgv_sku_fix.DataSource).Rows.Remove(((DataTable)dgv_sku_fix.DataSource).Select("sku = '合计'")[0]);
            //然后对Tables[0]重新排序
            DataTable dt;
            DataView dv = ((DataTable)dgv_sku_fix.DataSource).DefaultView;
            //得到点击的列
            str_headertext = dgv_sku_fix.Columns[i_sku_fix_header].DataPropertyName.ToString().Trim();
            if (b_putaway_sort) //升序
            {
                dv.Sort = str_headertext + " DESC";
                b_putaway_sort = false;
            }
            else
            {
                dv.Sort = str_headertext;
                b_putaway_sort = true;
            }
            dt = dv.ToTable();
            dgv_sku_fix.DataSource = null;
            dgv_sku_fix.DataSource = dt;
            //增加合计行
            f_add_sku_fix();
        }


        private void dgv_sku_fix_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i_sku_fix_header = e.ColumnIndex;
        }

        private void dvg_pallet_Sorted(object sender, EventArgs e)
        {
            string str_headertext;
            //先删除合计行
            ((DataTable)dvg_pallet.DataSource).Rows.Remove(((DataTable)dvg_pallet.DataSource).Select("pack_code = '合计'")[0]);
            //然后对Tables[0]重新排序
            DataTable dt;
            DataView dv = ((DataTable)dvg_pallet.DataSource).DefaultView;
            //得到点击的列
            str_headertext = dvg_pallet.Columns[i_pallet_header].DataPropertyName.ToString().Trim();
            if (b_putaway_sort) //升序
            {
                dv.Sort = str_headertext + " DESC";
                b_putaway_sort = false;
            }
            else
            {
                dv.Sort = str_headertext;
                b_putaway_sort = true;
            }
            dt = dv.ToTable();
            dvg_pallet.DataSource = null;
            dvg_pallet.DataSource = dt;
            //增加合计行
            f_add_pallet();
        }

        private void dvg_pallet_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i_pallet_header = e.ColumnIndex;
        }

        private void dvg_location_code_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i_location_header = e.ColumnIndex;
        }

        private void dvg_location_code_Sorted(object sender, EventArgs e)
        {
            string str_headertext;
            //先删除合计行
            ((DataTable)dvg_location_code.DataSource).Rows.Remove(((DataTable)dvg_location_code.DataSource).Select("sku = '合计'")[0]);
            //然后对Tables[0]重新排序
            DataTable dt;
            DataView dv = ((DataTable)dvg_location_code.DataSource).DefaultView;
            //得到点击的列
            str_headertext = dvg_location_code.Columns[i_location_header].DataPropertyName.ToString().Trim();
            if (b_putaway_sort) //升序
            {
                dv.Sort = str_headertext + " DESC";
                b_putaway_sort = false;
            }
            else
            {
                dv.Sort = str_headertext;
                b_putaway_sort = true;
            }
            dt = dv.ToTable();
            dvg_location_code.DataSource = null;
            dvg_location_code.DataSource = dt;
            //增加合计行
            f_add_location();
        }

        private void dgv_batchno_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i_batchno_header = e.ColumnIndex;
        }

        private void dgv_batchno_Sorted(object sender, EventArgs e)
        {
            string str_headertext;
            //先删除合计行
            ((DataTable)dgv_batchno.DataSource).Rows.Remove(((DataTable)dgv_batchno.DataSource).Select("pack_code = '合计'")[0]);
            //然后对Tables[0]重新排序
            DataTable dt;
            DataView dv = ((DataTable)dgv_batchno.DataSource).DefaultView;
            //得到点击的列
            str_headertext = dgv_batchno.Columns[i_batchno_header].DataPropertyName.ToString().Trim();
            if (b_putaway_sort) //升序
            {
                dv.Sort = str_headertext + " DESC";
                b_putaway_sort = false;
            }
            else 
            {
                dv.Sort = str_headertext;
                b_putaway_sort = true;
            }
            dt = dv.ToTable();
            dgv_batchno.DataSource = null;
            dgv_batchno.DataSource = dt;
            //增加合计行
            f_add_batchno();
        }

        private void btn_batch_Click(object sender, EventArgs e)
        {
            w_inv001_batch wsingle = new w_inv001_batch();
            wsingle.ShowDialog();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            pl4.Visible = false;
        }

        private void dvg_location_code_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i_temp = dvg_location_code.CurrentRow.Index;
            string str_name = dvg_location_code.Columns[dvg_location_code.CurrentCell.ColumnIndex].Name.ToString().Trim();
            //if (str_name == "sku_1")
            //{

                //如果你想复制第二行第三列的值：
            CopyColumnValueToClipboard(dvg_location_code, i_temp, dvg_location_code.CurrentCell.ColumnIndex); // 第二列 单品码 sku_1 
            //}
        }

        //复制某一列值到剪贴板
        private void CopyColumnValueToClipboard(DataGridView dgv, int rowIndex, int columnIndex)
        {
            if (dgv.Rows.Count <= rowIndex || columnIndex < 0)
            {
                // 检查索引是否超出范围
                return;
            }

            string value = dgv.Rows[rowIndex].Cells[columnIndex].Value.ToString();
            if (value != null)
            {
                Clipboard.SetText(value.ToString().Trim());


                MessageBox.Show("已复制到剪贴板");
            }
        }

        private void dgv_batchno_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i_temp = dgv_batchno.CurrentRow.Index;
            string str_name = dgv_batchno.Columns[dgv_batchno.CurrentCell.ColumnIndex].Name.ToString().Trim();

            //复制某一列值到剪贴板
            CopyColumnValueToClipboard(dgv_batchno, i_temp, dgv_batchno.CurrentCell.ColumnIndex); 
            
        }

        private void dvg_location_code_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dvg_location_code.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dvg_location_code.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dvg_location_code.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dgv_batchno_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgv_batchno.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgv_batchno.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgv_batchno.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dgv_sku_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgv_sku.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgv_sku.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgv_sku.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dgv_sku_fix_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dgv_sku_fix.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dgv_sku_fix.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dgv_sku_fix.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        // dgv 鼠标右键
        private void dgv_sku_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu(); // 创建一个 ContextMenu
                MenuItem deleteRowItem = new MenuItem("删除行"); // 创建一个 MenuItem
                deleteRowItem.Click += dgv_sku_DeleteRowItem_Click; // 添加点击事件
                m.MenuItems.Add(deleteRowItem);

                int currentMouseOverRow = dgv_sku.HitTest(e.X, e.Y).RowIndex;   // 将 MenuItem 添加到 ContextMenu
                m.Show(dgv_sku, new Point(e.X, e.Y));                           // 显示 ContextMenu
            }
        }

        private void dgv_sku_DeleteRowItem_Click(object sender, EventArgs e)
        {

            w_dgv_deleterow wsingle = new w_dgv_deleterow();
            wsingle.ShowDialog();
            int i_row_start = w_dgv_deleterow.i_row_start-1;
            int i_row_end = w_dgv_deleterow.i_row_end-1;

            // 确保起始行和结束行在有效范围内
            if (i_row_start < 0 || i_row_end >= dgv_sku.Rows.Count || i_row_start > i_row_end)
            {
                MessageBox.Show("超出有效范围！");
                return;
            }

            // 从 DataGridView 中删除指定范围的行 (从后向前删除行，避免索引变化问题)
            for (int i = i_row_end; i >= i_row_start; i--)
            {
                dgv_sku.Rows.RemoveAt(i);
            }

        }

        private void dgv_sku_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i_temp = dgv_sku.CurrentRow.Index;
            string str_name = dgv_sku.Columns[dgv_sku.CurrentCell.ColumnIndex].Name.ToString().Trim();
            //if (str_name == "sku_1")
            //{

            //如果你想复制第二行第三列的值：
            CopyColumnValueToClipboard(dgv_sku, i_temp, dgv_sku.CurrentCell.ColumnIndex); // 第二列 单品码 sku_1 
        }

        private void dgv_sku_fix_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu(); // 创建一个 ContextMenu
                MenuItem deleteRowItem = new MenuItem("删除行"); // 创建一个 MenuItem
                deleteRowItem.Click += dgv_sku_fix_DeleteRowItem_Click; // 添加点击事件
                m.MenuItems.Add(deleteRowItem);

                int currentMouseOverRow = dgv_sku_fix.HitTest(e.X, e.Y).RowIndex;   // 将 MenuItem 添加到 ContextMenu
                m.Show(dgv_sku_fix, new Point(e.X, e.Y));                           // 显示 ContextMenu
            }
        }

        private void dgv_sku_fix_DeleteRowItem_Click(object sender, EventArgs e)
        {

            w_dgv_deleterow wsingle = new w_dgv_deleterow();
            wsingle.ShowDialog();
            int i_row_start = w_dgv_deleterow.i_row_start - 1;
            int i_row_end = w_dgv_deleterow.i_row_end - 1;

            // 确保起始行和结束行在有效范围内
            if (i_row_start < 0 || i_row_end >= dgv_sku_fix.Rows.Count || i_row_start > i_row_end)
            {
                MessageBox.Show("超出有效范围！");
                return;
            }

            // 从 DataGridView 中删除指定范围的行 (从后向前删除行，避免索引变化问题)
            for (int i = i_row_end; i >= i_row_start; i--)
            {
                dgv_sku_fix.Rows.RemoveAt(i);
            }

        }

        private void dgv_sku_fix_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i_temp = dgv_sku_fix.CurrentRow.Index;
            string str_name = dgv_sku_fix.Columns[dgv_sku_fix.CurrentCell.ColumnIndex].Name.ToString().Trim();
            //if (str_name == "sku_1")
            //{

            //如果你想复制第二行第三列的值：
            CopyColumnValueToClipboard(dgv_sku_fix, i_temp, dgv_sku_fix.CurrentCell.ColumnIndex); // 第二列 单品码 sku_1 
        }

        // 删除行
        private void dvg_location_code_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu(); // 创建一个 ContextMenu
                MenuItem deleteRowItem = new MenuItem("删除行"); // 创建一个 MenuItem
                deleteRowItem.Click += dvg_location_code_DeleteRowItem_Click; // 添加点击事件
                m.MenuItems.Add(deleteRowItem);

                int currentMouseOverRow = dvg_location_code.HitTest(e.X, e.Y).RowIndex;   // 将 MenuItem 添加到 ContextMenu
                m.Show(dvg_location_code, new Point(e.X, e.Y));                           // 显示 ContextMenu
            }
        }

        private void dvg_location_code_DeleteRowItem_Click(object sender, EventArgs e)
        {

            w_dgv_deleterow wsingle = new w_dgv_deleterow();
            wsingle.ShowDialog();
            int i_row_start = w_dgv_deleterow.i_row_start - 1;
            int i_row_end = w_dgv_deleterow.i_row_end - 1;

            // 确保起始行和结束行在有效范围内
            if (i_row_start < 0 || i_row_end >= dvg_location_code.Rows.Count || i_row_start > i_row_end)
            {
                MessageBox.Show("超出有效范围！");
                return;
            }

            // 从 DataGridView 中删除指定范围的行 (从后向前删除行，避免索引变化问题)
            for (int i = i_row_end; i >= i_row_start; i--)
            {
                dvg_location_code.Rows.RemoveAt(i);
            }

        }


        // 删除行
        private void dgv_batchno_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu(); // 创建一个 ContextMenu
                MenuItem deleteRowItem = new MenuItem("删除行"); // 创建一个 MenuItem
                deleteRowItem.Click += dgv_batchno_DeleteRowItem_Click; // 添加点击事件
                m.MenuItems.Add(deleteRowItem);

                int currentMouseOverRow = dgv_batchno.HitTest(e.X, e.Y).RowIndex;   // 将 MenuItem 添加到 ContextMenu
                m.Show(dgv_batchno, new Point(e.X, e.Y));                           // 显示 ContextMenu
            }
        }

        private void dgv_batchno_DeleteRowItem_Click(object sender, EventArgs e)
        {

            w_dgv_deleterow wsingle = new w_dgv_deleterow();
            wsingle.ShowDialog();
            int i_row_start = w_dgv_deleterow.i_row_start - 1;
            int i_row_end = w_dgv_deleterow.i_row_end - 1;

            // 确保起始行和结束行在有效范围内
            if (i_row_start < 0 || i_row_end >= dgv_batchno.Rows.Count || i_row_start > i_row_end)
            {
                MessageBox.Show("超出有效范围！");
                return;
            }

            // 从 DataGridView 中删除指定范围的行 (从后向前删除行，避免索引变化问题)
            for (int i = i_row_end; i >= i_row_start; i--)
            {
                dgv_batchno.Rows.RemoveAt(i);
            }

        }

        private void w_inv001_Enter(object sender, EventArgs e)
        {
            if (s_enter_flag == "1")
            {
                btn_Query_Click(sender, new EventArgs());

                tabControl1.SelectedTab = tp_location_code;
            }
            //else
            //{
            //    s_enter_flag = "1";
            //}
        }



        

        

        

    }
}